*&---------------------------------------------------------------------*
*& Report ZEX_LISTING_38                                               *
*&---------------------------------------------------------------------*
*& Created By: James Wood (james.wood@bowdarkconsulting.com)           *
*& Created On: 12/12/2008                                              *
*& Purpose:    This program demonstrates the definition and use of     *
*&             friendship relationships in ABAP Objects.               *
*&---------------------------------------------------------------------*
REPORT zex_listing_38.

* We must include this statement in order to make class lcl_child
* known to class lcl_parent. This is a classic example of the chicken
* and the egg. In order for us to be able to specify a friendship
* relationship between lcl_parent and lcl_child, class lcl_child must
* exist. However, we cannot define lcl_child without first defining
* class lcl_parent as this is where the private attribute
* "credit_card_no" is defined. So, we must define class lcl_child
* provisionally first using the DEFINITION DEFERRED addition to the
* CLASS statement. This provides the compiler with the information it
* needs to go ahead and define the friendship relationship.
CLASS lcl_child DEFINITION DEFERRED.

*----------------------------------------------------------------------*
*       CLASS lcl_parent DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_parent DEFINITION FRIENDS lcl_child.
  PRIVATE SECTION.
    DATA: credit_card_no TYPE string VALUE '1234567890'.
ENDCLASS.                    "lcl_parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_child DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_child DEFINITION.
  PUBLIC SECTION.
    METHODS buy_toys.
ENDCLASS.                    "lcl_child DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_child IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_child IMPLEMENTATION.
  METHOD buy_toys.
    DATA: lr_parent TYPE REF TO lcl_parent.
    CREATE OBJECT lr_parent.
    WRITE: 'Credit card number from parent:',
           lr_parent->credit_card_no.
  ENDMETHOD.                    "buy_toys
ENDCLASS.                    "lcl_child IMPLEMENTATION

*----------------------------------------------------------------------*
* START-OF-SELECTION Event Module                                      *
*----------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM test_friendship.

*&---------------------------------------------------------------------*
*&      Form  test_friendship
*&---------------------------------------------------------------------*
FORM test_friendship.

* Local Data Declarations:
  DATA: lr_child TYPE REF TO lcl_child.

* Test the friendship relationship between classes
* lcl_parent and lcl_child.
  CREATE OBJECT lr_child.
  lr_child->buy_toys( ).

ENDFORM.                    "test_friendship